package com.liuyz.convert_;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class 转换输入流 {

    // 读取ANSI编码的文件
    @Test
    public void convertStreamRead() {
        // 文件编码为ANSI
        String path = "d:\\test.txt";
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            // 构建转换流，指定读取编码为GB2313（对应的就是ANSI）
            // inputStreamReader = new InputStreamReader(new FileInputStream(path));
            inputStreamReader = new InputStreamReader(new FileInputStream(path), "GB2312");
            // 构建缓冲字符流，将转换流传入，底层使用的还是文件字节输入流来读取文件
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedReader) {
                try {
                    // 关闭外层的缓冲字符流即可
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
